package market

import (
	"git.mycaigou.com/gfyx/common/authorize"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/market"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/market/outbound_task"
	"git.myscrm.cn/golang/stark/v4"
	"github.com/gin-gonic/gin"
)

type OutboundTaskController struct {
	controller.BaseController
	outboundTaskService outbound_task.OutboundTaskServiceIface
}

// NewOutboundTaskController 外呼任务
func NewOutboundTaskController(
	outboundTaskService outbound_task.OutboundTaskServiceIface,
) *OutboundTaskController {
	return &OutboundTaskController{
		outboundTaskService: outboundTaskService,
	}
}

func (c *OutboundTaskController) RegisterRouter(g *gin.RouterGroup) {
	// 导入人群包
	g.POST("task/import-crowd", c.taskImport)
	// 已完成和外呼线索列表接口
	g.POST("clue/list", c.clueList)
	// 外呼线索导出接口
	g.POST("clue/export", c.clueExport)
	// 外呼线索详情
	g.GET("clue/detail", c.clueDetail)

	g.POST("task/create", c.CreateTask)
	g.POST("task/update", c.UpdateTask)
	g.POST("task/delete", c.DeleteTask)
	g.POST("task/execute", c.ExecuteTask)
	g.GET("task/detail", c.Detail)
	g.GET("task/un-start-call-list", c.GetUnStartCallList)
	g.GET("task/list", c.GetList)
	g.POST("task/update-intention", c.UpdateIntention)

}

func (c *OutboundTaskController) taskImport(ctx *gin.Context) {
	var req dto.TaskImportCrowdRequest
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	req.CompanyId, _ = utils.GetByCompanyId(ctx, authorize.GetTenantCode(ctx))
	if req.CompanyId == 0 {
		c.Fail(ctx, errcode.OutboundCompanyIdNotExistError, "Company Id 未设置，请先设置之后再重新尝试")
		return
	}
	err := c.outboundTaskService.ImportCrowd(ctx, &req)
	if err != nil {
		stark.Logger.Errorf(ctx, "http taskImport ImportCrowd request:%+v, err:%s", req, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	c.Success(ctx, "ok")
}

func (c *OutboundTaskController) clueList(ctx *gin.Context) {
	var req dto.GetOutboundClueListRequest
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	resp, err := c.outboundTaskService.GetOutboundClueList(ctx, &req)
	if err != nil {
		stark.Logger.Errorf(ctx, "http clueList GetOutboundClueList request:%+v, err:%s", req, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	c.Success(ctx, resp)
}

func (c *OutboundTaskController) clueExport(ctx *gin.Context) {
	var req dto.OutboundClueExportRequest
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	resp, err := c.outboundTaskService.ExportTaskLog(ctx, &req)
	if err != nil {
		stark.Logger.Errorf(ctx, "http clueExport request:%+v, err:%s", req, err.Error())
		c.Fail(ctx, errcode.COMMON_ERROR, err.Error())
		return
	}
	c.Success(ctx, resp)
}

func (c *OutboundTaskController) CreateTask(ctx *gin.Context) {
	// 1. 校验参数
	request := dto.CreateTaskRequest{}
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		stark.Logger.Info(ctx, "create-task: "+err.Error())
		return
	}
	var userId int64
	if val, ok := ctx.Value(constants.LoginUserId).(int64); ok {
		userId = val
	}
	companyId, err := utils.GetByCompanyId(ctx, authorize.GetTenantCode(ctx))
	if err != nil || companyId == 0 {
		c.Fail(ctx, errcode.OutboundCompanyIdNotExistError, "Company Id 未设置，请先设置之后再重新尝试")
		return
	}
	// 2，逻辑处理
	response, err := c.outboundTaskService.CreateTask(ctx, companyId, userId, &request)
	if err != nil {
		stark.Logger.Info(ctx, "CreateTask: "+err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 3, 返回结果
	c.Success(ctx, response)
}

func (c *OutboundTaskController) UpdateIntention(ctx *gin.Context) {
	// 1. 校验参数
	request := &dto.UpdateIntentionRequest{}
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		stark.Logger.Info(ctx, "UpdateIntention: "+err.Error())
		return
	}

	// 2，逻辑处理
	response, err := c.outboundTaskService.UpdateIntention(ctx, request)
	if err != nil {
		stark.Logger.Info(ctx, "UpdateTask: "+err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 3, 返回结果
	c.Success(ctx, response)
}

func (c *OutboundTaskController) UpdateTask(ctx *gin.Context) {
	// 1. 校验参数
	request := dto.UpdateTaskRequest{}
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		stark.Logger.Info(ctx, "UpdateTask: "+err.Error())
		return
	}
	var userId int64
	if val, ok := ctx.Value(constants.LoginUserId).(int64); ok {
		userId = val
	}
	companyId, err := utils.GetByCompanyId(ctx, authorize.GetTenantCode(ctx))
	if err != nil || companyId == 0 {
		c.Fail(ctx, errcode.OutboundCompanyIdNotExistError, "Company Id 未设置，请先设置之后再重新尝试")
		return
	}
	// 2，逻辑处理
	response, err := c.outboundTaskService.UpdateTask(ctx, companyId, userId, &request)
	if err != nil {
		stark.Logger.Info(ctx, "UpdateTask: "+err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 3, 返回结果
	c.Success(ctx, response)
}

func (c *OutboundTaskController) DeleteTask(ctx *gin.Context) {
	// 1. 校验参数
	request := dto.DeleteTaskRequest{}
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		stark.Logger.Info(ctx, "DeleteTask: "+err.Error())
		return
	}
	var userId int64
	if val, ok := ctx.Value(constants.LoginUserId).(int64); ok {
		userId = val
	}
	companyId, err := utils.GetByCompanyId(ctx, authorize.GetTenantCode(ctx))
	if err != nil || companyId == 0 {
		c.Fail(ctx, errcode.OutboundCompanyIdNotExistError, "Company Id 未设置，请先设置之后再重新尝试")
		return
	}
	// 2，逻辑处理
	response, err := c.outboundTaskService.DeleteTask(ctx, companyId, userId, &request)
	if err != nil {
		stark.Logger.Info(ctx, "DeleteTask: "+err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 3, 返回结果
	c.Success(ctx, response)
}

func (c *OutboundTaskController) ExecuteTask(ctx *gin.Context) {
	// 1. 校验参数
	request := dto.ExecuteTaskRequest{}
	if err := ctx.ShouldBind(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		stark.Logger.Info(ctx, "ExecuteTask: "+err.Error())
		return
	}
	var userId int64
	if val, ok := ctx.Value(constants.LoginUserId).(int64); ok {
		userId = val
	}
	companyId, err := utils.GetByCompanyId(ctx, authorize.GetTenantCode(ctx))
	if err != nil || companyId == 0 {
		c.Fail(ctx, errcode.OutboundCompanyIdNotExistError, "Company Id 未设置，请先设置之后再重新尝试")
		return
	}
	// 2，逻辑处理
	response, err := c.outboundTaskService.ExecuteTask(ctx, companyId, userId, &request)
	if err != nil {
		stark.Logger.Info(ctx, "ExecuteTask: "+err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 3, 返回结果
	c.Success(ctx, response)
}

func (c *OutboundTaskController) clueDetail(ctx *gin.Context) {
	req := dto.GetOutboundClueDetailRequest{}
	if err := ctx.ShouldBind(&req); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(req, err))
		return
	}

	resp, err := c.outboundTaskService.GetOutboundClueDetail(ctx, &req)
	if err != nil {
		stark.Logger.Errorf(ctx, "http clueDetail GetOutboundClueDetail request:%+v, err:%s", req, err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, resp)
}

// GetList 获取列表
func (c *OutboundTaskController) GetList(ctx *gin.Context) {
	// 1. 校验参数
	request := dto.GetTaskListRequest{}
	if err := ctx.ShouldBindQuery(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		stark.Logger.Info(ctx, "GetTaskList: "+err.Error())
		return
	}
	var userId int64
	if val, ok := ctx.Value(constants.LoginUserId).(int64); ok {
		userId = val
	}
	companyId, err := utils.GetByCompanyId(ctx, authorize.GetTenantCode(ctx))
	if err != nil || companyId == 0 {
		c.Fail(ctx, errcode.OutboundCompanyIdNotExistError, "Company Id 未设置，请先设置之后再重新尝试")
		return
	}
	// 2，逻辑处理
	response, err := c.outboundTaskService.GetTaskList(ctx, companyId, userId, &request)
	if err != nil {
		stark.Logger.Info(ctx, "GetTaskList: "+err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 3, 返回结果
	c.Success(ctx, response)
}

func (c *OutboundTaskController) Detail(ctx *gin.Context) {
	// 1. 校验参数
	request := dto.GetTaskDetailRequest{}
	if err := ctx.ShouldBindQuery(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		stark.Logger.Info(ctx, "GetTaskDetail: "+err.Error())
		return
	}
	var userId int64
	if val, ok := ctx.Value(constants.LoginUserId).(int64); ok {
		userId = val
	}
	companyId, err := utils.GetByCompanyId(ctx, authorize.GetTenantCode(ctx))
	if err != nil || companyId == 0 {
		c.Fail(ctx, errcode.OutboundCompanyIdNotExistError, "Company Id 未设置，请先设置之后再重新尝试")
		return
	}
	// 2，逻辑处理
	response, err := c.outboundTaskService.GetTaskDetail(ctx, companyId, userId, &request)
	if err != nil {
		stark.Logger.Info(ctx, "GetTaskDetail: "+err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 3, 返回结果
	c.Success(ctx, response)
}

func (c *OutboundTaskController) GetUnStartCallList(ctx *gin.Context) {
	// 1. 校验参数
	request := dto.GetUnStartCallListRequest{}
	if err := ctx.ShouldBindQuery(&request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		stark.Logger.Info(ctx, "GetUnStartCallList: "+err.Error())
		return
	}
	var userId int64
	if val, ok := ctx.Value(constants.LoginUserId).(int64); ok {
		userId = val
	}
	companyId, err := utils.GetByCompanyId(ctx, authorize.GetTenantCode(ctx))
	if err != nil || companyId == 0 {
		c.Fail(ctx, errcode.OutboundCompanyIdNotExistError, "Company Id 未设置，请先设置之后再重新尝试")
		return
	}
	// 2，逻辑处理
	response, err := c.outboundTaskService.GetUnStartCallList(ctx, companyId, userId, &request)
	if err != nil {
		stark.Logger.Info(ctx, "GetTaskDetail: "+err.Error())
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	// 3, 返回结果
	c.Success(ctx, response)
}
